//
//  TELocalizationSystem.m
//  ThaiExpress
//
//  Created by Le Quang Vinh on 7/11/13.

//  Copyright (c) 2013 Fruity. All rights reserved.
//

#import "TELocalizationSystem.h"

@implementation TELocalizationSystem

//Singleton instance
static TELocalizationSystem *_sharedLocalSystem = nil;

//Current application bungle to get the languages.

static NSBundle *bundle = nil;


+ (TELocalizationSystem *)sharedLocalSystem
{
	@synchronized([TELocalizationSystem class])
	{
		if (!_sharedLocalSystem){
			_sharedLocalSystem = [[TELocalizationSystem alloc] init];
		}
		return _sharedLocalSystem;
	}
	// to avoid compiler warning
	return nil;
}

+(id)alloc
{
	@synchronized([TELocalizationSystem class])
	{
		NSAssert(_sharedLocalSystem == nil, @"Attempted to allocate a second instance of a singleton.");
		_sharedLocalSystem = [super alloc];
		return _sharedLocalSystem;
	}
	// to avoid compiler warning
	return nil;
}


- (id)init
{
    if ((self = [super init]))
    {
		//empty.
		bundle = [NSBundle mainBundle];
	}
    return self;
}

// Gets the current localized string as in NSLocalizedString.
//
// example calls:
// AMLocalizedString(@"Text to localize",@"Alternative text, in case hte other is not find");
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment
{
	return [bundle localizedStringForKey:key value:comment table:nil];
}


// Sets the desired language of the ones you have.
// example calls:
// LocalizationSetLanguage(@"Italian");
// LocalizationSetLanguage(@"German");
// LocalizationSetLanguage(@"Spanish");
//
// If this function is not called it will use the default OS language.
// If the language does not exists y returns the default OS language.
- (void) setLanguage:(NSString*) l{
	NSLog(@"preferredLang: %@", l);
	
	NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
	
    
	if (path == nil)
		//in case the language does not exists
		[self resetLocalization];
	else
		bundle = [NSBundle bundleWithPath:path];
}

// Just gets the current setted up language.
// returns "es","fr",...
//
// example call:
// NSString * currentL = LocalizationGetLanguage;
- (NSString*) getLanguage{
    
	NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    
	NSString *preferredLang = [languages objectAtIndex:0];
    
	return preferredLang;
}

// Resets the localization system, so it uses the OS default language.
//
// example call:
// LocalizationReset;
- (void) resetLocalization
{
	bundle = [NSBundle mainBundle];
}

#pragma mark - Langugages
- (void)setDefaultLanguages
{
    if (![[NSUserDefaults standardUserDefaults] valueForKey:kTypeOfLanguage])
    {
        LocalizationSetLanguage(DEFAULT_LANGUAGE);
        [[NSUserDefaults standardUserDefaults] setValue:DEFAULT_LANGUAGE forKey:kTypeOfLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else
        LocalizationSetLanguage([[NSUserDefaults standardUserDefaults] valueForKey:kTypeOfLanguage]);
}

- (void)switchLanguages
{
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:kTypeOfLanguage] isEqualToString:OTHER_LANGUAGE])
    {
        LocalizationSetLanguage(DEFAULT_LANGUAGE);
        [[NSUserDefaults standardUserDefaults] setValue:DEFAULT_LANGUAGE forKey:kTypeOfLanguage];
    } else
    {
        LocalizationSetLanguage(OTHER_LANGUAGE);
        [[NSUserDefaults standardUserDefaults] setValue:OTHER_LANGUAGE forKey:kTypeOfLanguage];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
